package org.glut.service;

import org.glut.bean.HWJC;
import org.glut.serialport.manager.SerialPortManager;
import org.glut.serialport.ui.MainFrame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Scope("singleton")
public class HWJCService {
    @Autowired
    MessageService messageService;
    //    @Autowired
//    private  static MainFrame mainFrame;
    @Autowired
    public static HWJC hwjc1;
    public static HWJC hwjc2;
    public List<String> hwjctimeList1 = new ArrayList<>();
    public List<String> hwjcpower1 = new ArrayList<>();
    public List<String> hwjctimeList2 = new ArrayList<>();
    public List<String> hwjcpower2 = new ArrayList<>();
    private SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置时间

    public HWJCService() {
        hwjc1 = new HWJC();
        hwjc2 = new HWJC();
//        (HWJC)GetApplicationContext.getBean("HWJC")
    }


    public void getmsgs(String[] message) {
//        for (String s : message) {
//            System.out.println(s);
//        }
//        cfAir = (HWJC) GetApplicationContext.getBean("HWJC");
        char id = message[0].charAt(2);
        if (id == '1') {
            initCF(hwjc1,message,hwjctimeList1,hwjcpower1);
        } else if (id == '2') {
            initCF(hwjc2,message,hwjctimeList2,hwjcpower2);
        }


//            System.out.println(timeList);
//            System.out.println(cfList);
    }

    private void initCF(HWJC hwjc, String[] message, List<String> hwjctimeList, List<String> hwjcpower) {
//        数据帧 HW1 BXXXX 00X0 E
        hwjc.setId(message[0].charAt(2)+"");
        hwjc.setPower(message[1].substring(1));
        hwjc.setStatus(message[2].substring(3));
        String time = df.format(new Date());
        hwjctimeList.add(time);
        hwjcpower.add(hwjc.getPower());
    }
}
